package com.ftg.learn.controller;

import com.alibaba.fastjson2.JSON;
import com.ftg.learn.entity.Clazz;
import com.ftg.learn.service.ClazzService;
import lombok.SneakyThrows;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

/**
 * 班级控制器
 *  java 生产者   html css js jsp (消费者)
 * @author kangjx
 * @version 1.0
 * @date 2023/3/29 下午1:48
 */
@Controller
@RequestMapping("/clazz")
public class ClazzController {

    /**
     * 成功的拿到了spring 日志
     */
    private final Log log = LogFactory.getLog(ClazzController.class);

    @Resource
    ClazzService clazzService;

    @SneakyThrows
    @GetMapping("/showall")
    public void showall(HttpServletResponse resp){
        List<Clazz> list = clazzService.showall();
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");

        String json = JSON.toJSONString(list);

        PrintWriter writer = resp.getWriter();

        writer.print(json);

        writer.flush();
        writer.close();
    }

    @RequestMapping(value = "showall1",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    /**
     * ResponseBody : 响应  body 体
     *   报文
     * jackson
     */
    @ResponseBody
    public List<Clazz> showall1(){
        log.info("开始对班级进行查询操作");
        return  clazzService.showall();
    }

    //Rest Restful  restful api
}
